﻿using System.Collections;
using System.Collections.Generic;
using CBR.Core.Helpers;
using CBR.Core.Helpers.Localization;
using GalaSoft.MvvmLight.Messaging;

namespace CBR.ViewModels
{
	public class TocViewModel : ToolViewModel
	{
		#region ----------------CONSTRUCTOR----------------

		/// <summary>
		/// Constructor
		/// </summary>
		public TocViewModel()
            : base(CultureManager.Instance.GetLocalization("ByCode", "TocView.Title", "Table of content"))
		{
            this.ContentId = "TocViewModel";
			base.Icon = "pack://application:,,,/Resources/Images/32x32/book/book.png";
			base.IsVisible = false;

			CultureManager.Instance.UICultureChanged += new CultureEventArrived(Instance_UICultureChanged);

			Messenger.Default.Register<DocumentViewModel>(this, ViewModelMessages.DocumentActivChanged, HandleActiveDocumentChange);
			//Messenger.Default.Register<object>(ViewModelMessages.TocContentChanged, HandleTocContentChange);
		}

		/// <summary>
		/// Child classes can override this method to perform clean-up logic, such as removing event handlers.
		/// </summary>
		override public void Cleanup()
		{
			base.Cleanup();
	
			CultureManager.Instance.UICultureChanged -= new CultureEventArrived(Instance_UICultureChanged);

			Messenger.Default.Unregister<DocumentViewModel>(this, ViewModelMessages.DocumentActivChanged, HandleActiveDocumentChange);
			//Messenger.Default.Unregister<object>(ViewModelMessages.TocContentChanged, HandleTocContentChange);
		}

		#endregion

		new public BookViewModelBase Data
		{
			get { return base.Data as BookViewModelBase; }
			set
			{
				base.Data = value;
			}
		}

		#region -----------------HANDLERS-----------------

		private void Instance_UICultureChanged(object sender, CultureEventArgs e)
		{
            DisplayName = CultureManager.Instance.GetLocalization("ByCode", "TocView.Title", "Table of content");
		}

		//private void HandleTocContentChange(object o)
		//{
		//    if (o is IList)
		//    {
		//        TableOfContent = (List<object>)o;
		//        base.IsVisible = true;
		//    }
		//    else
		//        base.IsVisible = false;
		//}

		private void HandleTocContentChange(BookViewModelBase o)
		{
			if (o != null)
			{
				if (o.HasTableOfContent)
				{
					Data = o;
					base.IsVisible = true;
				}
				
				RaisePropertyChanged("TableOfContent");
			}
			else
			{
				base.IsVisible = false;
				Data = null;
			}
		}

		private void HandleActiveDocumentChange(DocumentViewModel o)
		{
			if (o is BookViewModelBase)
			{
				HandleTocContentChange(o as BookViewModelBase);
			}
			else HandleTocContentChange(null);
		}

		#endregion

		#region -----------------PROPERTIES-----------------

		public IList TableOfContent
        {
            get
            {
				return Data != null ? Data.TableOfContent : null;
            }
        }

        #endregion

		#region -----------------PROPERTIES-----------------
		
		/// <summary>
		/// Notify views about a toc change
		/// </summary>
		/// <param name="tocData"></param>
		public void NotifyColleagues(object tocData)
		{
			//Messenger.Default.Send(ViewModelMessages.TocIndexChanged, tocData);
			Data.TableOfContentIndex = tocData;
		}

		#endregion
	}
}
